package com.yc.cloudvideo.sp_user.controller;


import cn.hutool.crypto.SecureUtil;
import com.yc.cloudvideo.entity.User;
import com.yc.cloudvideo.sp_user.mapper.UserMapper;
import com.yc.cloudvideo.sp_user.service.impl.UserServiceImpl;
import com.yc.common.bean.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LYYT
 * @since 2022-08-03
 */
@RestController
@RequestMapping("/cloudvideo/user")
public class UserController {

    /**
     * 注册账号
     */
    @Resource
    private UserServiceImpl userService;

    @PostMapping("create")
    public Map<String, Object> createAccount(User user) {
        return userService.createAccount(user);
    }

    @PostMapping("log")
    public Map<String, Object> loginAccount(User user, HttpSession session) {
        return userService.loginAccount(user, session);
    }

    /**
     * 激活账号
     *
     * @param confirmCode
     * @return
     */
    @GetMapping("activation")
    public Map<String, Object> activationAccount(String confirmCode) {
        return userService.activationAccount(confirmCode);
    }

    @RequestMapping("logout")
    public Result logout(HttpSession session) {
        //会话失效
        session.invalidate();
        return new Result(1, "退出成功", null);
    }


    //http://127.0.0.1:8003/cloudvideo/user/getLoginedUser
    @GetMapping("getLoginedUser")
    public Result getLoginedUser(HttpSession session) {
        Object loginedUser = session.getAttribute("loginedUser");
        System.out.println("loginedUser=" + loginedUser);
        if (loginedUser == null) {
            return new Result(0, "用户未登录", null);
        } else {
            return new Result(1, "用户已登录", loginedUser);
        }

    }


    /**
     * ======================================================================
     */
    @Resource
    UserMapper userMapper;

    //获取仓库拥有者信息
    @PostMapping("selectStoreOwner")
    User selectStoreOwner(@RequestParam("cid") int cid) {
        return userMapper.selectStoreOwner(cid);
    }

    //获取仓库贡献者信息
    @PostMapping("getUsersGByCid")
    List<User> getUsersGByCid(int cid) {
        List<User> users = userMapper.selectUserGByCid(cid);
        return users;
    }

    //获取用户信息
    @PostMapping("getUserByUid")
    @ResponseBody
    User getUsersGByUid(int uid){
        User user = userMapper.selectUserByUid(uid);
        return user;
    }

    @PostMapping("saveHead")
    Result saveHead(String head, @SessionAttribute User loginedUser ){
        loginedUser.setHead(head);
        User user = new User();
        user.setUid( loginedUser.getUid());
        user.setHead(head);

        userMapper.updateById(user);
        return new Result(1,"头像更新成功！",null);

    }

    @PostMapping("updateUserInfo")
    public Result updateUserInfo(String nickName,String sign,boolean isBoy,Integer age,@SessionAttribute User loginedUser,HttpSession session){
        userMapper.updateUserInfo(nickName,sign,isBoy,age,loginedUser.getUid()+"");
        User user=userMapper.selectById(loginedUser.getUid());
        session.setAttribute("loginedUser",user);
        return new Result(1,"保存信息成功");
    }

    @PostMapping("updatePwd")
    @ResponseBody
    public int updatePwd(String uid, String password, String newPassword, @SessionAttribute User loginedUser) {

        int updateFlag = 0;

        if(password == null || newPassword == null){
            return 0;
        }
        String newP = SecureUtil.md5(String.valueOf(newPassword.getBytes()));
        final User user = userMapper.selectByUid(loginedUser.getUid()+"");
        if(user != null){
            String pwd = user.getPassword();
            if(pwd != null){
                try {
                    // 成功返回1
                    updateFlag = userMapper.updatePwd(newP,String.valueOf(user.getUid()));

                } catch (Exception e) {
                    // 不成功返回0，数据库回滚
                    updateFlag = 2;
                }
            }else{
                // 旧密码错误
                updateFlag = 3;
            }
        } else {
            // 用户不存在
            updateFlag = 4;
        }
        return updateFlag;
    }

}

